package com.mvp.shq.washer.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.entity.shop.ShopSpec;
import com.mvp.shq.washer.view.widget.CustomGridView;
import com.mvp.shq.washer.view.widget.LayoutManagerFlowLayout.FlowLayoutManager;
import com.mvp.shq.washer.view.widget.LayoutManagerFlowLayout.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/10/10.
 */

public class ShopSpecAdapter extends RecyclerView.Adapter<ShopSpecAdapter.ViewHolder> {


    private Context context;
    private List<ShopSpec> data;
    private OnAttrClickListener listener;
    private boolean Multiselect = false;
    private List<ShopAttrFlowAdapter> adapters;

    public ShopSpecAdapter(Context mContext,List<ShopSpec> list,OnAttrClickListener clickListener){
        context = mContext;
        data = list;
        listener = clickListener;
        adapters = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_spec, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ShopSpec spec = data.get(position);
        holder.tvSpecName.setText(spec.getSpecName());
        final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) holder.recyclerView.getLayoutParams();
        lp.width = context.getResources().getDisplayMetrics().widthPixels;
        final FlowLayoutManager flowLayoutManager = new FlowLayoutManager();
        holder.recyclerView.addItemDecoration(new SpaceItemDecoration(dp2px(10)));
        holder.recyclerView.setLayoutManager(flowLayoutManager);
        ShopAttrFlowAdapter adapter = new ShopAttrFlowAdapter(context,data.get(position).getAttrs());
        int specPosition = position;
        adapter.setListener(new ShopAttrFlowAdapter.OnAttrItemClickListener() {
            @Override
            public void onClick(int position) {
                boolean isselect = data.get(specPosition).getAttrs().get(position).isSelect();
                if (!Multiselect && isselect){
                    return;
                }
                if (!Multiselect){
                    for (int i=0;i<data.get(specPosition).getAttrs().size();i++){
                        data.get(specPosition).getAttrs().get(i).setSelect(false);
                    }
                }
                data.get(specPosition).getAttrs().get(position).setSelect(!isselect);
                adapter.notifyDataSetChanged();
                listener.onClick(specPosition,position);
            }
        });
        adapters.add(adapter);
        holder.recyclerView.setAdapter(adapter);
        holder.recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                holder.recyclerView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                Log.d("wocao", "flowLayoutManager.getTotalHeight():" + flowLayoutManager.getTotalHeight());
                lp.height = flowLayoutManager.getTotalHeight();// + holder.recyclerView.getPaddingBottom() + holder.recyclerView.getPaddingTop();
                holder.recyclerView.setLayoutParams(lp);
            }
        });

        holder.gvAttr.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                listener.onClick(specPosition,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setMultiselect(boolean multiselect) {
        Multiselect = multiselect;
    }

    public void reset(){
        for (int i=0;i<adapters.size();i++){
            adapters.get(i).notifyDataSetChanged();
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_spec_name)
        TextView tvSpecName;
        @BindView(R.id.gv_attr)
        CustomGridView gvAttr;
        @BindView(R.id.recyclerview)
        RecyclerView recyclerView;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public interface OnAttrClickListener{
        void onClick(int specPosition,int attrPosition);
    }

    private int dp2px(float value) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics());
    }
}
